package org.learn.java.mq.producer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.logging.SimpleFormatter;

import org.learn.java.mq.MqContent;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.SendResult;

public class ProducerTest {

	public static int messageKey = 0;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		generalsend(); //普通消息发送

	}

	public static void generalsend() {
		// 配置producer参数
		Properties properties = new Properties();
		properties.put(PropertyKeyConst.ProducerId, "PID_t_1");
		properties.put(PropertyKeyConst.AccessKey, MqContent.MQ_AccessKey );
		properties.put(PropertyKeyConst.SecretKey,
				"7JbgLiklyNxmkXQkM0iBKWuyqNvnjU");
		// 依据参数创建producer
		Producer producer = ONSFactory.createProducer(properties);
		// 启动producer
		producer.start();
		// 组装消息
		String messageBody = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:sss")
				.format(new Date());
		Message message = new Message("Topic_t_1", "m1",
				messageBody.getBytes());
		// 设置消息唯一字段
		message.setKey(String.valueOf(System.currentTimeMillis()));
		//发送延时消息
		message.setStartDeliverTime(System.currentTimeMillis()+30000);
		// 消息发送
		SendResult sendResult = producer.send(message);
		System.out.println(sendResult.getMessageId());
		producer.shutdown();
	}

}
